#ifndef CUSTOMIZEDOPENGLWIDGET_H
#define CUSTOMIZEDOPENGLWIDGET_H

#include <QtOpenGLWidgets/QOpenGLWidget>
#include <QObject>
#include <QtOpenGL/QOpenGLFunctions_3_3_Core>
#include <QMouseEvent>
#include <QPoint>
#include <QVector>
#include <QtOpenGL/QOpenGLShaderProgram>


class CustomizedOpenGLWidget
    : public QOpenGLWidget, QOpenGLFunctions_3_3_Core
{
    Q_OBJECT
public:
    explicit CustomizedOpenGLWidget(QWidget *parent = nullptr);

    enum shape{
        None,
        Rect,
        Circle,
        Triangle
    };

public slots:
    void startDrawRect(bool state);
    void startDrawTriangle(bool state);
    void setPolygonMode(bool state);
    void clearCanvas(bool state);

protected:
    void initializeGL() override;
    void paintGL() override;
    void resizeGL(int w, int h) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;

private:
    unsigned int VBO, VAO, EBO;
    // 渲染模式
    bool polygonMode;

    // 着色器元素
    QOpenGLShaderProgram shaderProgram;
    QPoint lastPos;
    QPoint mousePressPos;
    QPoint currentPos;
    bool isMousePressed;
    shape currentShape;
    QVector<float> vertices;
    QVector<int> indices;

private slots:
    float getNormalizedX(int x);
    float getNormalizedY(int y);
    void drawRect(QPoint beginPos, QPoint endPos);
    void drawTriangle(QPoint beginPos, QPoint endPos);
};

#endif // CUSTOMIZEDOPENGLWIDGET_H
